package pe.sonrisas.tools.mysql;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import pe.sonrisas.tools.dao.PacienteDAO;
import pe.sonrisas.tools.dao.PersonaDAO;
import pe.sonrisas.tools.entities.Modulo;

public class ModuloDAOMysql {
    Conexion_BD co=new Conexion_BD();
    ResultSet rs = null;
    CallableStatement cs;
    
    
    
    public boolean agregarModulo(Modulo nuevoModulo){
        boolean inserto = false;
        
        try {
            co.Conectar();
            co.getCn().setAutoCommit(false);
            //Llamamos el Store Procedure 
            cs = co.getCn().prepareCall("{CALL usp_registrar_modulo(?,?,?)}");
            //Enviamos parametros al Store Procedure
            cs.setString(1, nuevoModulo.getNombre());
            cs.setString(2, nuevoModulo.getDescripcion());
            cs.setInt(3, nuevoModulo.getIdUsuario());
            
            
            //Ejecutamos el Store Procedure
            cs.executeUpdate();
            //Comiteamos
            co.getCn().commit();
            inserto = true;    
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
            try {
                co.getCn().rollback();
            } catch (SQLException ex1) {
                Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
        
        
        finally {
            try {
                co.getCn().setAutoCommit(true);
                co.Desconectar();
                
            } catch (Exception ex) {
                Logger.getLogger(PersonaDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
                    }
                        return inserto;
                        
                                                    }
    
    
    public ArrayList<ArrayList> listarTodos(){
        ArrayList<ArrayList> listado = new ArrayList<ArrayList>();       
        try {
            
            co.Conectar();
            rs = co.Consultar("select idModulo,nombre,descripcion,FechaCreacion from modulo where activoModulo = 1");
            if (rs.first()) {
                while (!rs.isAfterLast()) {
                    ArrayList item = new ArrayList();
                    item.add(rs.getString(1));
                    item.add(rs.getString(2));
                    item.add(rs.getString(3));
                    item.add(rs.getString(4));
                     
                    //Avanza el RS
                    rs.next();
                    listado.add(item);
                    
                    
                }
            }

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }
                    return listado;
        
    }
    
    
    public boolean editarModulo(Modulo nuevoModulo,String viejoNombre){
        boolean inserto = false;

        try {
            co.Conectar();
            co.getCn().setAutoCommit(false);
            
            //Llamamos el Store Procedure 
            cs = co.getCn().prepareCall("{CALL usp_modificar_modulo(?,?,?)}");
            //Enviamos parametros al Store Procedure
            cs.setString(1, nuevoModulo.getNombre());
            cs.setString(2, nuevoModulo.getDescripcion());
            cs.setString(3, viejoNombre);
            
            
            //Ejecutamos el Store Procedure
            cs.executeUpdate();
            //Comiteamos
            co.getCn().commit();
            inserto = true;      
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }

                    return inserto;


    }
    
    
    public boolean eliminarModulo(String nombre){
        
        boolean inserto = false;
        
        try {
            co.Conectar();
            co.getCn().setAutoCommit(false);           
            //Llamamos el Store Procedure 
            cs = co.getCn().prepareCall("{CALL usp_eliminar_modulo(?)}");
            //Enviamos parametros al Store Procedure
            cs.setString(1, nombre);
                      
            //Ejecutamos el Store Procedure
            cs.executeUpdate();
            //Comiteamos
            co.getCn().commit();
            inserto = true;      
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        try {
            co.Desconectar();
        } catch (Exception ex) {
            Logger.getLogger(ModuloDAOMysql.class.getName()).log(Level.SEVERE, null, ex);
        }

                    return inserto;
     
    }
       
}

